from pymysql import connect, cursors

USERNAME = 'root'
PASSWORD = 'jisuanji2022'
HOST = '121.196.166.117'
PORT = 3306
DATABASE = "woniunote"

def query_data(sql_str):
    try:
        # 连接数据库
        conn = connect(user=USERNAME, password=PASSWORD, host=HOST, port=PORT, database=DATABASE)

        # 获取游标对象cursor
        cur = conn.cursor(cursors.DictCursor)

        # 使用游标对象来执行SQL语句
        row_count = cur.execute(sql_str)

        # 提交修改数据的SQL语句到数据库
        conn.commit()

        # 使用游标对象获取查询结果
        result = cur.fetchall()

    except Exception as e:
        print(e)
        # result = None  # 发生异常时返回None

    finally:
        # 关闭游标
        cur.close()

        # 断开与数据库的连接
        conn.close()

    return result